package com.wang.springbootinit.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.springbootinit.annotation.AuthCheck;
import com.wang.springbootinit.common.BaseResponse;
import com.wang.springbootinit.common.DeleteRequest;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.common.ResultUtils;
import com.wang.springbootinit.constant.UserConstant;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.exception.ThrowUtils;
import com.wang.springbootinit.model.dto.chart.ChartAddRequest;
import com.wang.springbootinit.model.dto.chart.ChartEditRequest;
import com.wang.springbootinit.model.dto.chart.ChartQueryRequest;
import com.wang.springbootinit.model.dto.chart.ChartUpdateRequest;
import com.wang.springbootinit.model.entity.Chart;
import com.wang.springbootinit.model.entity.User;
import com.wang.springbootinit.model.vo.ChartInfoVO;
import com.wang.springbootinit.model.vo.ChartVO;
import com.wang.springbootinit.service.ChartService;
import com.wang.springbootinit.service.UserService;
import com.wang.springbootinit.utils.ChartQueryWrapperUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/chart")
@Slf4j
public class ChartController {

    @Resource
    private ChartService chartService;
    @Resource
    private UserService userService;

    /**
     * 获取首页图表信息
     * @param request
     * @return
     */
    @PostMapping("/get/chartInfo")
    public BaseResponse<ChartInfoVO> getChartInfo(HttpServletRequest request){
        return ResultUtils.success(chartService.getChartInfo(request));
    }
    /**
     * 创建(添加)
     *
     * @param chartAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addChart(@RequestBody ChartAddRequest chartAddRequest, HttpServletRequest request) {
        if (chartAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long addChart = chartService.addChart(chartAddRequest, request);
        return ResultUtils.success(addChart);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteChart(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean deleteResultBoolean = chartService.deleteChart(deleteRequest, request);
        return ResultUtils.success(deleteResultBoolean);
    }

    /**
     * 更新（仅管理员）
     *
     * @param chartUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateChart(@RequestBody ChartUpdateRequest chartUpdateRequest) {
        if (chartUpdateRequest == null || chartUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = chartService.updateChart(chartUpdateRequest);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取图表
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<ChartVO> getChartById(Long id, HttpServletRequest request) {
        if (ObjectUtil.isNull(id) || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ChartVO chart = chartService.getChartById(id, request);
        return ResultUtils.success(chart);
    }

    /**
     * 分页获取列表（封装类）
     * 搜索
     *
     * @param chartQueryRequest
     * @param request
     * @return
     */
    @ApiOperation("分页列表查询")
    @PostMapping("/list/page")
    public BaseResponse<Page<Chart>> listChartByPage(@RequestBody ChartQueryRequest chartQueryRequest, HttpServletRequest request) {
        long current = chartQueryRequest.getCurrent();
        long size = chartQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        User user = userService.getUser(request);
        Page<Chart> chartPage = chartService.page(new Page<>(current, size), ChartQueryWrapperUtils.getQueryWrapper(chartQueryRequest,user));
        return ResultUtils.success(chartPage);
    }

    /**
     * 模糊查询 查询分析图表信息
     *
     * @param chartQueryRequest
     * @param request
     * @return
     */
    @ApiOperation("个人用户模糊查询")
    @PostMapping("/my/list/search")
    public BaseResponse<Page<Chart>> searchListChartByPage(@RequestBody ChartQueryRequest chartQueryRequest, HttpServletRequest request) {
        Page<Chart> chartPage = chartService.searchListChartByPage(chartQueryRequest, request);
        return ResultUtils.success(chartPage);
    }


    /**
     * 分页获取当前用户创建的资源列表
     * 只能获取到当前登录用户的信息
     *
     * @param chartQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page")
    public BaseResponse<Page<Chart>> listMyChartByPage(@RequestBody ChartQueryRequest chartQueryRequest, HttpServletRequest request) {
        if (chartQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Chart> chartPage = chartService.listMyChartByPage(chartQueryRequest, request);
        return ResultUtils.success(chartPage);
    }

    /**
     * 返回当前用户的图表状态和图表数据 图表分析目标 图表名
     */
    @PostMapping("/my/list/chartStatus")
    public BaseResponse<List<ChartVO>> getChartStatusList(HttpServletRequest request) {
        List<ChartVO> chartStatusVOList = chartService.getChartStatusList(request);
        return ResultUtils.success(chartStatusVOList);
    }


    /**
     * 编辑（用户）
     *
     * @param chartEditRequest
     * @param request
     * @return
     */
    @PostMapping("/edit")
    public BaseResponse<Boolean> editChart(@RequestBody ChartEditRequest chartEditRequest, HttpServletRequest request) {
        if (chartEditRequest == null || chartEditRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean result = chartService.editChart(chartEditRequest, request);
        return ResultUtils.success(result);
    }
}